package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwakT;
import cn.e4j.modular.crm.entity.GwbyT;
import cn.e4j.modular.crm.mapper.GwbyTMapper;
import cn.e4j.modular.crm.service.GwbyTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuwei
 * @since 2019-11-18
 */
@Service
public class GwbyTServiceImpl extends ServiceImpl<GwbyTMapper, GwbyT> implements GwbyTService {

    @Autowired
    SysAuthorityService sysAuthorityService;

    @Override
    public List<Map<String, Object>> getGwbylist(Page<Object> page, Dto inDto) {

        //客户权限
        inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
        //部门权限
        inDto.put("ORGS", sysAuthorityService.getOrgsAuthority(inDto));

        List<Map<String, Object>> list = InvokeUtil.invokeBeanMethod("gwbyTMapper,getGwbylist",page,inDto);
        return list;
    }

    @Override
    public List<GwbyT> getGwbyDetailList(String id) {
        QueryWrapper<GwbyT> gwbyTQueryWrapper = new QueryWrapper<>();
        gwbyTQueryWrapper.eq("GWBY001", id);
        return this.list(gwbyTQueryWrapper);
    }

    @Override
    public boolean existedTheParams(GwbyT gwbyT) {
        QueryWrapper<GwbyT> gwbyTQueryWrapper = new QueryWrapper<>();
        gwbyTQueryWrapper.eq("GWBY001", gwbyT.getGWBY001());
        return this.baseMapper.selectList(gwbyTQueryWrapper).size() > 0;
    }

    @Override
    @Transactional
    public void add(List<GwbyT> gwbyTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbyTList.forEach(gwbyT -> gwbyT.setGWBYENT(shiroUser.getDBC()));
        this.saveBatch(gwbyTList);
    }

    @Override
    @Transactional
    public void edit(List<GwbyT> gwbyTList) {
        this.delete(gwbyTList.get(0));
        this.add(gwbyTList);
    }

    @Override
    @Transactional
    public void delete(GwbyT gwbyT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        UpdateWrapper<GwbyT> gwbyTQueryWrapper = new UpdateWrapper<>();
        gwbyTQueryWrapper.eq("GWBYENT", shiroUser.getDBC());
        gwbyTQueryWrapper.eq("GWBY001", gwbyT.getGWBY001());
        gwbyT.delete(gwbyTQueryWrapper);
    }

    @Override
    @Transactional
    public void deleteBatch(List<GwbyT> gwbyTList) {
        for (GwbyT gwbyT : gwbyTList) {
            delete(gwbyT);
        }
    }

    @Override
    @Transactional
    public void changeStatusBatch(List<GwbyT> gwbyTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        GwbyT updateGwbyT = new GwbyT();
        String state = gwbyTList.get(0).getGWBYACTI();
        updateGwbyT.setGWBYACTI(state);

        if(state.equals("Y")){
            updateGwbyT.setGWBY005(LocalDateTime.now());
        }else if(state.equals("T")) {
            updateGwbyT.setGWBY004(LocalDateTime.now());
        }else if (state.equals("N")){
            updateGwbyT.setGWBY004(null);
        }

        for (GwbyT gwbyT : gwbyTList) {
            if(state.equals("Y")){
                updateGwbyT.setGWBY004(gwbyT.getGWBY004());
            }
            UpdateWrapper<GwbyT> gwbyTQueryWrapper = new UpdateWrapper<>();
            gwbyTQueryWrapper.eq("GWBYENT", shiroUser.getDBC());
            gwbyTQueryWrapper.eq("GWBY001", gwbyT.getGWBY001());
            updateGwbyT.update(gwbyTQueryWrapper);
        }
    }

    @Override
    @Transactional
    public void updateGwak001(List<GwbyT> gwbyTList) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwbyTList.forEach(gwbyT -> {
            GwakT updateGwakT = new GwakT();
            updateGwakT.setGWAK011(judgeGrade(gwbyT.getGWBY003()));
            UpdateWrapper<GwakT> gwakTQueryWrapper = new UpdateWrapper<>();
            gwakTQueryWrapper.eq("GWAKENT", shiroUser.getDBC());
            gwakTQueryWrapper.eq("GWAK001", gwbyT.getGWBY002());
            updateGwakT.update(gwakTQueryWrapper);
        });
    }

    public String judgeGrade(int score) {
        if(score >= 90) {
            return "AAA";
        }else if(score>=80) {
            return "AA";
        }else if(score>=70) {
            return "A";
        }else if(score>=60) {
            return "BBB";
        }else if(score>=50) {
            return "BB";
        }else {
            return "B";
        }
    }

    @Override
    public List<Map<String, Object>> getGwbyData(Page<Object> page, Dto inDto) {
        List<Map<String, Object>> list = InvokeUtil.invokeBeanMethod("gwbyTMapper,getGwbyData", page, inDto);
        return list;
    }
}